import {defineStore} from 'pinia'
import axios from 'axios'

export const useTeamStore = defineStore('Team',{
    actions:{
        fetchtoken(token){
            this.token = token
        },
        async fetchteamgroupid(id){
          if (!this.token) return; // 如果token为空，则不执行后续操作
                try {
                  // 发送POST请求到后端接口
                  const response = await axios.post('http://back1.idcfengye.com:15173/teamgroup',{}, {
                    headers: {
                      Authorization: `Bearer ${this.token}`
                    }
                  });  
                  if(response.data.status == 200 || response.data.success){
                    if(id==0){
                    ElMessage.success('您的队伍是'+response.data.data.teamName);
                    }
                  }
                  this.teamGroupId =response.data.data.id,
                  localStorage.setItem('teamid',this.teamGroupId);                    
                } catch (error) {
                  if (error.response.success || error.response.status == 404) {  
                    this.teamGroupId='';                  
                    ElMessage.error('您还没有队伍请前往创建队伍');
                  }
                }
                if (!this.teamGroupId) return; // 如果teamGroupId为空，则不执行后续操作
                try {
                  const response = await axios.get(`http://back1.idcfengye.com:15173/competition/staff/${this.teamGroupId}`, {
                    headers: {
                      'Authorization': `Bearer ${this.token}`
                    }
                  });
              
                  if (response.data.status == 200 || response.data.success) {
                    this.staffs = response.data.staffs;
                  }
                } catch (error) {
                  if (error.response.status == 404|| error.response.success) {
                  }
                }
        }
    },
    //储存数据
    state(){
        return {
            teamGroupId:'',
            staffs:[],//队员
            token: localStorage.getItem('userToken')//获取token
        } 
    }
})